/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2020-05-06 14:55:11
 * @LastEditors: kl2018
 * @LastEditTime: 2020-08-10 10:26:25
 */
import { message } from 'antd';

const styleObj = {
  'font-size': '18px',
};

export const convertSpecialChar = (str) => {
  str = str.toString();
  str = str.replace(/<br>/ig, '\n');
  str = str.replace(/&nbsp;/ig, ' ');
  str = str.replace(/&lt;/ig, '<');
  str = str.replace(/&gt;/ig, '>');
  str = str.replace(/&quot;/ig, '"');
  str = str.replace(/&amp;/ig, '&');
  return str;
};

export const showGlobalMessage = (msg, delaysec = 1) => {
  if (typeof msg == 'string') {
    message.success(<span style={styleObj} dangerouslySetInnerHTML={{ __html: msg }} />, delaysec);
  } else {
    message.success(<span style={styleObj} />, delaysec);
  }
};

export const showGlobalError = (msg, delaysec = 1) => {
  if (typeof msg == 'string') {
    message.error(<span style={styleObj} dangerouslySetInnerHTML={{ __html: msg }} />, delaysec);
  } else {
    message.error(<span style={styleObj}>{msg}</span>, delaysec);
  }
};

// 根据字段id集合获取对应字段值
export const getRelateFieldValues = (fieldidArr = [], rowIndex, layoutStore) => {
  let result = {};
  const { mainData, detailMap, fieldAttrMap } = layoutStore;
  fieldidArr.map((fieldid) => {
    let fieldValue = '';
    if (fieldAttrMap.has(fieldid.toString())) {
      const tableMark = fieldAttrMap.get(fieldid.toString()).tableMark;
      if (tableMark === 'main') {
        fieldValue = mainData.has(`field${fieldid}`) ? mainData.get(`field${fieldid}`).value : '';
      } else if (tableMark.indexOf('detail_') > -1) {
        const detailObj = detailMap.get(tableMark);
        const detailData = detailObj.detailData;
        fieldValue = detailData.has(`field${fieldid}_${rowIndex}`) ? detailData.get(`field${fieldid}_${rowIndex}`).value : '';
      }
    }
    result[fieldid] = fieldValue.toString();
  });
  return result;
};

// 根据字段id取对应字段值
export const getRelateFieldValue = (fieldid, rowIndex, layoutStore) => {
  const fieldidArr = [fieldid];
  const relateFieldValues = getRelateFieldValues(fieldidArr, rowIndex, layoutStore);
  return relateFieldValues[fieldid] || '';
};

